package com.random.fox.whatspay.greendao.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import com.random.fox.whatspay.bean.Categories;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.AbstractDaoSession;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.DaoConfig;

/**
 * Created by 渠 on 2014/11/11.
 */
public class CategoriesDao extends AbstractDao<Categories, Long> {
    public static final String TABLENAME = "categories";

    public static class Properties{
        public final static Property Id = new Property(0,Long.class,"id",true,"_id");
        public final static Property  ParentId = new Property(1,Long.class,"parentId",false,"PARENT_ID");
        public final static Property Name = new Property(2,String.class,"name",false,"NAME");
    }

    public CategoriesDao(DaoConfig config) {
        super(config);
    }

    public CategoriesDao(DaoConfig config,AbstractDaoSession daoSession){
        super(config,daoSession);
    }

    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'categories' (" + //
                "'_id' INTEGER PRIMARY KEY AUTOINCREMENT," + // 0: id
                "'PARENT_ID' INTEGER,"+  // parent id 可能是null的表示为第一级
                "'NAME' TEXT NOT NULL )"); // 1: name
    }

    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'categories'";
        db.execSQL(sql);
    }

    @Override
    protected void bindValues(SQLiteStatement stmt, Categories categories) {
        stmt.clearBindings();

        Long parentId = categories.getParentId();
        if (parentId != null){
            stmt.bindLong(2,parentId);
        }
        stmt.bindString(3,categories.getName());
    }

    @Override
    protected Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }

    @Override
    protected Categories readEntity(Cursor cursor, int offset) {
        Categories categories = new Categories();
        readEntity(cursor,categories,offset);
        return categories;
    }

    @Override
    protected void readEntity(Cursor cursor, Categories categories, int offset) {
        categories.setId(cursor.isNull(offset+0)?null:cursor.getLong(offset +0));
        categories.setParentId(cursor.isNull(offset+1)?null:cursor.getLong(offset+1));
        categories.setName(cursor.getString(offset+2)); //因为从数据库设计的时候，这个字段判断为非NULL
    }

    @Override
    protected Long updateKeyAfterInsert(Categories categories, long rowId) {
        categories.setId(rowId);
        return rowId;
    }

    @Override
    protected Long getKey(Categories categories) {
        if (categories!=null){
            return categories.getId();
        }else {
            return null;
        }
    }

    @Override
    protected boolean isEntityUpdateable() {
        return true;
    }

}
